package com.material.mdbook;

import android.app.Application;
import android.content.Context;
import android.support.v7.app.AppCompatDelegate;

import com.avos.avoscloud.AVOSCloud;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.material.mdbook.exception.CrashHandler;
import com.material.mdbook.ui.activity.BaseActivity;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

/**
 * Created by hymanme on 2016/1/5 0005.
 * Application
 */
public class BaseApplication extends Application {
    public final static String TAG = "BaseApplication";
    public final static boolean DEBUG = true;
    private static BaseApplication application;
    private static int mainTid;
    /**
     * Activity集合，来管理所有的Activity
     */
    private static List<BaseActivity> activities;

    public static final String HUAWEI_APP_ID = "26rlBe7sL13QY9ypAdwVObmD-gzGzoHsz";
    public static final String HUAWEI_KEY = "9cQt8xUXUREEbHBxHnFfTMQk";
    public static final String HUAWEI_OBJECT_ID = "5caae931a91c93158a2603a1";

    public static final String XIAOMI_APP_ID = "2KEDmzCB5nIeNtXjO3hH6hYy-gzGzoHsz";
    public static final String XIAOMI_KEY = "ft7r76WYSx60SXeL733K4FGx";
    public static final String XIAOMI_OBJECT_ID = "5cac3e800237d700680b343d";

    static {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        activities = new LinkedList<>();
        application = this;
        mainTid = android.os.Process.myTid();

        initLeanCloud();
        initOkgo();

        CrashHandler crashHandler = new CrashHandler();
        crashHandler.init(this);
    }

    private void initLeanCloud() {
        AVOSCloud.initialize(this, XIAOMI_APP_ID, XIAOMI_KEY);
    }


    private void initOkgo() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        //log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);

        //全局的读取超时时间
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        //全局的写入超时时间
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);
        //全局的连接超时时间
        builder.connectTimeout(5 * 1000, TimeUnit.MILLISECONDS);

        OkGo.getInstance().init(this)
                .setOkHttpClient(builder.build())
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .setRetryCount(0);
    }

    /**
     * 获取application
     *
     * @return
     */
    public static Context getApplication() {
        return application;
    }

    /**
     * 获取主线程ID
     *
     * @return
     */
    public static int getMainTid() {
        return mainTid;
    }

    /**
     * 添加一个Activity
     *
     * @param activity
     */
    public void addActivity(BaseActivity activity) {
        activities.add(activity);
    }

    /**
     * 结束一个Activity
     *
     * @param activity
     */
    public void removeActivity(BaseActivity activity) {
        activities.remove(activity);
    }

    /**
     * 结束当前所有Activity
     */
    public static void clearActivities() {
        ListIterator<BaseActivity> iterator = activities.listIterator();
        BaseActivity activity;
        while (iterator.hasNext()) {
            activity = iterator.next();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    /**
     * 退出应运程序
     */
    public static void quiteApplication() {
        clearActivities();
        System.exit(0);
    }
}
